<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_product extends MY_Controller {

	function Admin_product()
	{
		parent::MY_Controller();
		parent::check_admin_login();
	}

	function index()
	{
		$this->template->view('v1/index');
	}

    function product_new() {
        $this->load->model('mproduct');
		$types = $this->mproduct->get_product_types();
        $this->template->view('admin/product_add',array('types'=>$types));
    }

    function product_add() {
        $form = array('p_name','p_days','p_type','price','dc_type','discount','p_desc','week_num','week_money','wb_discount','porder');
        foreach($form as $val){
            $$val = $this->input->post($val);
        }
        if(!$wb_discount){
        	$wb_discount = '0';
        }
    	if(!$porder){
        	$porder = '1';
        }
        if(!$p_name || !$p_days || !$p_type) {
            echo '你有必填项目没有填写！';exit;
        }
        $values = array();
        foreach($form as $val) {
            $values[$val] = $$val;
        }
        $values['add_time'] = date('Y-m-d H:i:s');
        $this->load->model('mproduct');
        if($this->mproduct->add_new_product($values)) {
            echo '产品：'.$p_name.' 添加成功！';
        } else {
            echo '产品添加失败！';
        }
    }

	function product_list($type=0,$offset=0){
		$this->load->model('Mproduct');
		$temp = $this->Mproduct->get_product_types();
        $types = array();
        foreach($temp as $arr){
            $types[$arr['id']] = $arr['type_name'];
        }
        if($type == 0){
        	$type = $temp[0]['id'];
        }
        $total = $this->Mproduct->get_product_count($type);
        //echo $total;
		$this->load->library('pagination');

		$config['base_url'] = base_url().'admin_product/product_list/'.$type;
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();

		$products = $this->Mproduct->get_product_list($type,$config['per_page'],$offset);
		$this->template->view('admin/product_list',array('products'=>$products,'types'=>$types,'page_html'=>$page_html));
	}

    function product_edit($id='') {
        if($id === '') {
            echo '请选择产品后再修改！';return;
        }
        $this->load->model('mproduct');
        $types = $this->mproduct->get_product_types();
        $info = $this->mproduct->get_product_info($id);
        $this->template->view('admin/product_edit',array('info'=>$info,'types'=>$types));
    }

    function product_modify() {
        $id = $this->input->post('id');
        $form = array('p_name','p_days','p_type','price','dc_type','discount','p_desc','week_num','week_money','wb_discount','porder');
        foreach($form as $val){
            $$val = $this->input->post($val);
        }
        if(!$id || !$p_name || !$p_days || !$p_type) {
            echo '你有必填项目没有填写！';exit;
        }
    	if(!$wb_discount){
        	$wb_discount = '0';
        }
    	if(!$porder){
        	$porder = '1';
        }
        $values = array();
        foreach($form as $val) {
            $values[$val] = $$val;
        }
        $values['add_time'] = date('Y-m-d H:i:s');
        $this->load->model('mproduct');
        if($this->mproduct->update_product_info($id,$values)) {
            echo '产品：'.$p_name.' 更新成功！';
        } else {
            echo '产品更新失败！';
        }
    }

    function product_view(){
    	//
    }

    function product_delete($id='') {
        if($id === '') {
            echo '没有选择产品，请返回重试！';return;
        }
        $this->load->model('mproduct');
        if($this->mproduct->delete_product($id)) {
            echo '产品删除成功！';
        } else {
            echo '产品删除失败！';
        }
    }

    function type_admin() {
        $this->load->model('mproduct');
        $types = $this->mproduct->get_product_types();
        $this->template->view('admin/type_list',array('types'=>$types));
    }

    function type_new() {
        $this->template->view('admin/type_add');
    }

    function type_add() {
        $type_name = $this->input->post('type_name');
        if(!$type_name) {
            echo '请填写分类名称！';return;
        }
        $values = array('type_name'=>$type_name);
        $this->load->model('mproduct');
        if($this->mproduct->add_new_type($values)) {
            echo '新分类： '.$type_name.' 添加成功！';
        } else {
            echo '添加失败！';
        }
    }

    function type_edit($id='') {
        if($id === '') {
            echo '请先选择分类！';return;
        }
        $this->load->model('mproduct');
        $info = $this->mproduct->get_type_info($id);
        $this->template->view('admin/type_edit',array('info'=>$info));
    }

    function type_modify() {
        $id = $this->input->post('id');
        $type_name = $this->input->post('type_name');
        if(!$id || !$type_name) {
            echo '有必填项没填，请返回重新填写！';return;
        }
        $this->load->model('mproduct');
        if($this->mproduct->update_type_info($id,array('type_name'=>$type_name))) {
            echo '更新成功！ 新分类名称为：'.$type_name;
        } else {
            echo '更新失败！';
        }
    }

    function type_delete($id=''){
    	if($id === '') {
            echo '选择分类后方可继续';return;
        }
        $this->load->model('mproduct');
        if($this->mproduct->delete_type($id)) {
            echo '删除成功！';
        } else {
            echo '删除失败！';
        }
    }

}

/* End of file product_admin.php */
/* Location: ./system/application/controllers/product_admin.php */
?>